<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用 URL 编码给定的字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.curl-error.html">« curl_error</a></li>
      <li style="float: right;"><a href="function.curl-exec.html">curl_exec »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.curl.html">cURL 函数</a></li>
    <li>使用 URL 编码给定的字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.curl-escape" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">curl_escape</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">curl_escape</span> &mdash; <span class="dc-title">使用 URL 编码给定的字符串</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.curl-escape-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>curl_escape</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$ch</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   该函数使用 URL 根据<a href="http://www.faqs.org/rfcs/rfc3986" class="link external">&raquo;&nbsp;RFC 3986</a>编码给定的字符串。
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.curl-escape-parameters">
  <h3 class="title">参数</h3>
  <dl>
   <dt>
<code class="parameter">handle</code>
</dt>
<dd>
<p class="para">由 <span class="function"><a href="function.curl-init.html" class="function">curl_init()</a></span> 返回的 cURL 句柄。</p></dd>

   
    <dt>
<code class="parameter">str</code></dt>

    <dd>

     <p class="para">
      需要编码的字符串
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.curl-escape-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回编码后的字符串 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.curl-escape-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4195">
    <p><strong>示例 #1 <span class="function"><strong>curl_escape()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;创建一个&nbsp;curl&nbsp;句柄<br /></span><span style="color: #0000BB">$ch&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">curl_init</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;把编码后的字符串当做一个&nbsp;GET&nbsp;参数<br /></span><span style="color: #0000BB">$location&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">curl_escape</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Hofbräuhaus&nbsp;/&nbsp;München'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;结果：&nbsp;Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen<br /><br />//&nbsp;用编码好的字符串组装一个&nbsp;URL<br /></span><span style="color: #0000BB">$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"http://example.com/add_location.php?location=</span><span style="color: #007700">{</span><span style="color: #0000BB">$location</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;结果：&nbsp;http://example.com/add_location.php?location=Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen<br /><br />//&nbsp;发送&nbsp;HTTP&nbsp;请求并关闭句柄<br /></span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLOPT_URL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$url</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLOPT_RETURNTRANSFER</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">curl_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">curl_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.curl-escape-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.curl-unescape.html" class="function" rel="rdfs-seeAlso">curl_unescape()</a> - 解码给定的 URL 编码的字符串</span></li>
    <li class="member"><span class="function"><a href="function.urlencode.html" class="function" rel="rdfs-seeAlso">urlencode()</a> - 编码 URL 字符串</span></li>
    <li class="member"><span class="function"><a href="function.rawurlencode.html" class="function" rel="rdfs-seeAlso">rawurlencode()</a> - 按照 RFC 3986 对 URL 进行编码</span></li>
    <li class="member"><a href="http://www.faqs.org/rfcs/rfc3986" class="link external">&raquo;&nbsp;RFC 3986</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>